package me.ezitku.base.extensions.permission

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment
import androidx.core.content.ContextCompat

/**
 * Created by yurqi on 2017/5/18.
 * ھوقۇق
 */
typealias mani = Manifest.permission

//ھوقۇق ئىلتىماس قىلىش
fun Activity.requestPermiss(permissions: Array<String>, requestCode: Int = 0) {
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)
        ActivityCompat.requestPermissions(this, permissions, requestCode)
}

fun Fragment.requestPermiss(permissions: Array<String>, requestCode: Int = 0) {
    activity!!.requestPermiss(permissions, requestCode)
}

//PackageManager.PERMISSION_DENIED //رەت قىلىنغان
//PackageManager.PERMISSION_GRANTED//ھوقۇق بېرىلگەن
//ھوقۇق تەكشۈرۈش
fun Context.checkPermiss(permission: String): Boolean {
    return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this, permission)
}

fun Context.checkPermiss(permission: String, action: () -> Unit): Boolean {
    if (checkPermiss(permission)) {
        action()
        return true
    } else {
        return false
    }

}